/**
 *  文件名:ActivityAccountInfo.java
 *  修改人:lihangjie
 *  创建时间:2015-8-30 下午4:55:48
 */
package com.sharemob.tinchat.modules.account;

import java.util.ArrayList;

import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.sharemob.tinchat.R;
import com.sharemob.tinchat.base.BaseActivity;
import com.sharemob.tinchat.base.Item;
import com.sharemob.tinchat.base.ListAdapter;
import com.sharemob.tinchat.base.LocalSheetDialog;
import com.sharemob.tinchat.base.LocalSheetDialog.OnSheetItemClickListener;
import com.sharemob.tinchat.base.LocalSheetDialog.SheetItemColor;
import com.sharemob.tinchat.base.NoScrollListview;
import com.sharemob.tinchat.lib.Utils;
import com.sharemob.tinchat.lib.common.SMGlobal;
import com.sharemob.tinchat.modules.dao.cache.CacheManager;
import com.sharemob.tinchat.modules.dao.entity.Account;
import com.sharemob.tinchat.modules.launch.MainActivity;
/**
 * 
 * <一句话功能简述>
 *
 * @author lihangjie
 * version [版本号,2015-8-30 下午4:55:48]
 * @see    [相关类/方法]
 * @since  [产品/模块版本]
 *
 */
public class ActivityAccountInfo extends BaseActivity implements AMapLocationListener{
//    private String[] names=new String[]{"昵称","个性签名","性别","地区"};
//    private String[] tags=new String[]{"nickname","voice","sex","area"};
	private Handler handler = null;
	private ListAdapter listAdapter=null;
	private NoScrollListview icon_listview,info_listview;
	private ArrayList<Item> icon_list = new  ArrayList< Item>(); 
	private ArrayList<Item> info_list = new  ArrayList< Item>(); 
	
	private Account account=CacheManager.getInstance().getAccount();
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_account_info);
        
        exitView(R.id.title_back,R.anim.activity_close,0);

        init();
        //设置个人资料头像
        Item icon=new Item();
        icon.setId("icon");
        icon.setIcon(false);
        icon.setArrow_icon(true);
        
        String pathString=String.format("%s.jpg", SMGlobal.getIconPath(account.getUnionid()));
        System.out.println(pathString);
        icon.setMidIcon(Utils.getDiskBitmap(pathString));
        icon.setName("头像"); 
        icon_list.add(icon);
        icon_listview= getInitListView(icon_list,R.id.listview_account_icon,true);
        
        
        Item nickname=new Item();
        nickname.setId("nickname");
        nickname.setIcon(false);
        nickname.setArrow_icon(true);
        nickname.setDrawLeft(0);
        nickname.setRightName(account.getNickname());
        nickname.setName("昵称"); 
        info_list.add(nickname);
        
        
        Item signature=new Item();
        signature.setId("signature");
        signature.setIcon(false);
        signature.setArrow_icon(true);
        if(null==account.getSignature()||"".equals(account.getSignature())){
        	 signature.setRightName("你也真懒!");
        }else{
        	signature.setRightName(account.getSignature());
        }
       
        signature.setName("文字签名"); 
        info_list.add(signature);
        
        Item signature_voice=new Item();
        signature_voice.setId("signature_voice");
        signature_voice.setIcon(false);
        signature_voice.setArrow_icon(true);
        signature_voice.setRightName("尝试录制吧");
        signature_voice.setName("语音签名"); 
        info_list.add(signature_voice);
        
//        
        Item sex=new Item();
        sex.setId("sex");
        sex.setIcon(false);
        sex.setArrow_icon(true);
        sex.setRightName(account.getSexCN());
        sex.setName("性别"); 
        info_list.add(sex);
        
        Item age=new Item();
        age.setId("birthday");
        age.setIcon(false);
        age.setArrow_icon(true);
        age.setRightName(account.getBirthday());
        age.setName("生日"); 
        info_list.add(age);
        
        Item constellation=new Item();
        constellation.setId("constellation");
        constellation.setIcon(false);
        constellation.setArrow_icon(false);
        constellation.setRightName(account.getConstellation());
        constellation.setName("星座"); 
        info_list.add(constellation);
//        
        Item area=new Item();
        area.setId("area");
        area.setIcon(false);
        area.setArrow_icon(false);
        area.setDrawLeft(R.drawable.city_location);
        String province_city=String.format("定位中  ..."); 
        area.setRightName(province_city);
        area.setName("地区"); 
        info_list.add(area);
   
        info_listview= getInitListView(info_list,R.id.listview_account,true);
   
    	NoScrollListview listView=(NoScrollListview)findViewById(R.id.listview_account);
		listAdapter=new ListAdapter();
		listAdapter.setAdapter(info_list);
		listView.setAdapter(listAdapter);
	
		listView.setOnItemClickListener(new OnItemClickListener(){
	            public void onItemClick(AdapterView<?> parent, View view, int position,long id)
	            {
	            	keyEventOfListView(position,parent.getId());
	            }
	     });
		//接收用户更新信息
		handler = new Handler() {  
            public void handleMessage(Message msg) {  
                super.handleMessage(msg);  
                Bundle bundle=msg.getData();
                switch (msg.what) {  
                	case KeyNickname:
                		String nickname=bundle.getString("nickname");
                		System.out.println("----nickname----"+nickname);
                		for(Item item:info_list){
                			if("nickname".equals(item.getId())){
                				item.setRightName(nickname);
                			}
                		}
                		listAdapter.notifyDataSetChanged();
                		break;
                	case KeySex:
                		int sex=bundle.getInt("sex");
                		for(Item item:info_list){
                			if("sex".equals(item.getId())){
                				switch (sex) {
								case 1:item.setRightName("男");
									break;
								case 2:item.setRightName("女");
									break;
								case 3:item.setRightName("人妖");
									break;
								default:
									break;
								}
                				
                			}
                		}
                		System.out.println(sex);
                		listAdapter.notifyDataSetChanged();
                        break;
                	case KeyBirthday:
                		String birthday=bundle.getString("birthday");
                		String constellation=bundle.getString("constellation");
                		System.out.println("----birthday----"+birthday);
                		for(Item item:info_list){
                			if("birthday".equals(item.getId())){
                				item.setRightName(birthday);
                			}else if("constellation".equals(item.getId())){
                				item.setRightName(constellation);
                			}
                		}
                		listAdapter.notifyDataSetChanged();
                        break;
                	case KeySignature:
                		String signatrue=bundle.getString("signatrue");
                		for(Item item:info_list){
                			if("signature".equals(item.getId())){
                				item.setRightName(signatrue);
                			}
                		}
                		listAdapter.notifyDataSetChanged();
                        break;
                }  
            }  
        }; 
        SMGlobal.getInstance().handler=handler;
        /* 
         * 添加线程到消息队列，如果该Handler被触发就会执行其消息队列中的线程 
         */  
//        handler.post(update);  
    }
    public static final int KeyNickname=1;
    public static final int KeySex=2;
	public static final int KeyBirthday=3;
	public static final int KeySignature=4;
	
	protected void keyEventOfListView(int position, int id) {
		switch (id) {
		case R.id.listview_account_icon:{
			Item tag=icon_list.get(position);
			System.out.println("----"+tag.getId());
			if("icon".equals(tag.getId())){
//				DialogSelectPhoto selectPhoto=new DialogSelectPhoto(this);
//				selectPhoto.show();
				Utils.SelectPhotoDialog(ActivityAccountInfo.this);
				
			}
		}
			break;
		case R.id.listview_account:
		{
			Item tag=info_list.get(position);
			if("nickname".equals(tag.getId())){
				gotoView(this,ActivityNickname.class);
			}else if("sex".equals(tag.getId())){
				gotoView(this,SelectSexActivity.class);
			}else if("signature".equals(tag.getId())){
				gotoView(this,SignatureActivity.class);
			}
			else if("signature_voice".equals(tag.getId())){
				gotoView(this,SignatureVoiceActivity.class);
			}else if("birthday".equals(tag.getId())){
				gotoView(this,BirthdayActivity.class);
			}else if("constellation".equals(tag.getId())){
				gotoView(this,BirthdayActivity.class);
			}else if("area".equals(tag.getId())){
				gotoViewByAnim(this, LocationActivity.class, R.anim.in_from_right, false);
			}
		}
			break;

		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		handler=null;
		SMGlobal.getInstance().handler=null;
	}

	@Override
	public void onLocationChanged(Location location) {
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		
	}

	protected void onPause() {
		super.onPause();
		// 移除定位请求
		mLocationManagerProxy.removeUpdates(this);
		// 销毁定位
		mLocationManagerProxy.destroy();
	}

	@Override
	public void onLocationChanged(AMapLocation amapLocation) {
		if (amapLocation != null&& amapLocation.getAMapException().getErrorCode() == 0) {
			// 定位成功回调信息，设置相关消息
			for(Item item:info_list){
    			if("area".equals(item.getId())){
    				String address=String.format("%s %s %s %s",amapLocation.getProvince(),amapLocation.getCity(),amapLocation.getDistrict(),amapLocation.getPoiName() );
    				CacheManager.getInstance().getAccount().setAddress(address);
    				item.setRightName(address);
    			}
    		}
    		listAdapter.notifyDataSetChanged();
			
		}
	}
	
	/**
	 * 初始化定位
	 */
	private void init() {
		// 初始化定位，只采用网络定位
		mLocationManagerProxy = LocationManagerProxy.getInstance(this);
		mLocationManagerProxy.setGpsEnable(false);
		// 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
		// 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用removeUpdates()方法来取消定位请求
		// 在定位结束后，在合适的生命周期调用destroy()方法
		// 其中如果间隔时间为-1，则定位只定一次,
		// 在单次定位情况下，定位无论成功与否，都无需调用removeUpdates()方法移除请求，定位sdk内部会移除
		mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 60 * 1000, 15, this);

	}
	private LocationManagerProxy mLocationManagerProxy;
}
